/**
 * 满意度打分
 */
 
QZONE.widget.dig = {
	
	_showed : true,
	
	_timeoutToClose:null,
	
	defaultConfig : {
		'id' : 1,
		'uin' : 0,
		'digName' : '分享功能',
		'aoneUp' : '好用',
		'aoneDown' : '不好用',
		'about' : '<a href="http://qzone.qq.com" target="_blank">关于分享</a>',
		'suggest' : '<a href="http://qzone.qq.com" target="_blank">提建议</a>',
		'container' : 'container',
		'tween':false,
		'downTips' : '',
		'oz':0
	},
	
	setup : function(config){
		var dig = QZONE.widget.dig,digc = QZONE.widget.dig.defaultConfig, g_StyleID = 1;
		for(key in config){
			digc[key] = config[key];
		}
		if(QZFL.userAgent.ie<8 ){
			dig.defaultConfig.tween = false;
		}
	    if (typeof(QZONE.FP._t.g_StyleID) != "undefined") {
	        g_StyleID = QZONE.FP._t.g_StyleID;
	    }else{
	    	g_StyleID = 1;
	    }
	    if((QZONE.FP._t.checkLogin()==QZONE.FP._t.g_iUin)&&(QZONE.cookie.get("skey"))){
			QZONE.css.insertCSSLink("http://imgcache.qq.com/qzonestyle/global/css/dig_lite.css");
			QZONE.widget.dig.getDigData();
		}else{
			QZONE.FP.showLoginBox("mall");//这里不让他刷新空间
		}
	},
	
	getDigData : function(){
		var get_survey_data = 'http://users.qzone.qq.com/cgi-bin/survey/get_survey_data.cgi',
		dig = QZONE.widget.dig,digc = QZONE.widget.dig.defaultConfig;
		var sender = new QZONE.JSONGetter(get_survey_data, "get_survey_data",{uin:digc.uin,id:digc.id,quessum:1,ans:2,rd:Math.random()}, "utf-8");
		sender.onSuccess = function(re){
			if(re.error==undefined){
				dig.renderDigData(re);
			}else{
				if(re.error.type=='login'){
					QZONE.FP.showLoginBox("mall");
				}else{
					dig.showMsgbox(re.error.msg,1,2000);
				}
			}
		};
		sender.onError = function(){
			this.showMsgbox("操作失败，请稍后再试。",1,2000);
		};
		sender.send("_Callback");
	},
	
	renderDigData : function(re,show){
		var dig = QZONE.widget.dig,digc = QZONE.widget.dig.defaultConfig,container = $(digc.container),template = digc.template,t,myDigClass,myDigTitle,abit,totalTips="",qone,up=0,down=0,percentUp=0,percentDown=0,up_show,down_show,total=0;
		show = show || false;
		if(re.answer!=null){
			abit = re.answer.toString().split('_')[0]-0;
		}else{
			abit = -1;
		}
		
		up = re.sum[0].split('_')[0]-0;
		down = re.sum[0].split('_')[1]-0;
		total = up+down;
		totalTips = dig.getTotalTips(total);
		percentUp = (total==0?0 : dig.fourFive(up,total));
		percentDown = (total==0?0 : (100 -percentUp));
		up_show = dig.getNumShow(up);
		down_show = dig.getNumShow(down);
		
		t = '<div id="dig-lite" class="gb_dig_lite">'+
				''+digc.digName+'：<a href="javascript:;" class="'+(abit==-1?"dl_btn_up":"dl_btn_up_gray")+'" '+(abit==-1?'onclick="QZONE.widget.dig.setDigData(1);return false;"':'')+' title="'+digc.aoneUp+'"><span id="up-add" class="dl_add1 c_tx4">+1</span></a> ('+up_show+') <a href="javascript:;" class="'+(abit==-1?"dl_btn_down":"dl_btn_down_gray")+'" '+(abit==-1?'onclick="QZONE.widget.dig.setDigData(2);return false;"':'')+' title="'+digc.aoneDown+'"><span id="down-add" class="dl_add1 c_tx4">+1</span></a> ('+down_show+') <a id="dig-triangle" href="javascript:;" class="dl_btn_result bor2 c_tx" title="查看结果">▼</a>'+
				'<div class="dl_result '+(show?'':'none')+'" id="dig-result"><div class="dl_tit">'+digc.digName+'评价</div>'+
					'<div class="dl_cont">'+
						'<div class="dl_tit_sub">'+(abit==-1?(digc.downTips.length>0?digc.downTips:'<span class="right" onclick="QZONE.widget.dig.sendPV(1);">'+digc.about+'</span>'+totalTips+'为你推荐“'+digc.digName+'”'):('我的评价：<span class="dl_btn_'+(abit==1?'up':'down')+'" title="'+(abit==1?digc.aoneUp:digc.aoneDown)+'"> </span>&nbsp;'+(abit==1?digc.aoneUp:digc.aoneDown)))+'</div>'+
						'<table width="100%" border="0" cellspacing="0" cellpadding="0">'+
							'<colgroup style="width:50px;"></colgroup><colgroup style="width:170px;"></colgroup>'+
							'<tr>'+
								'<th>'+digc.aoneUp+'：</th>'+
								'<td><div class="dl_bar_up"><div class="dl_bar_range"><div class="dl_pro" id="t-up" style="width:'+(digc.tween?"0px":percentUp+"%")+'"><div class="dl_pro_bor"></div></div></div></div></td>'+
								'<td class="dl_count">'+up+'('+percentUp+'%)</td>'+
							'</tr>'+
							'<tr>'+
								'<th>'+digc.aoneDown+'：</th>'+
								'<td><div class="dl_bar_down"><div class="dl_bar_range"><div class="dl_pro" id="t-down" style="width:'+(digc.tween?"0px":percentDown+"%")+'"><div class="dl_pro_bor"></div></div></div></div></td>'+
								'<td class="dl_count">'+down+'('+percentDown+'%)</td>'+
							'</tr>'+
							'<tr><td></td><td colspan="2">共有 '+total+' 人进行评价</td></tr>'+
						'</table>'+
						'<div class="dl_op">'+(abit==-1?"":'<span onclick="QZONE.widget.dig.sendPV(1);">'+digc.about+'</span>')+'<span onclick="QZONE.widget.dig.sendPV(2);">'+digc.suggest+'</span></div>'+
					'</div>'+
				'</div>'+
			'</div>';
		container.innerHTML = t;
		
		QZFL.event.addEvent($('dig-triangle'), "click", function(evt){
			dig.show(evt,percentUp,percentDown);
		});

		QZONE.event.addEvent($("dig-triangle"), "blur", function(){setTimeout(dig.hide,500);});
		
		QZFL.event.addEvent($('dig-lite'), "mouseover", function(evt){
			clearTimeout(dig._timeoutToClose);
		});
		QZFL.event.addEvent($('dig-lite'), "mouseout", function(evt){
			dig._timeoutToClose = setTimeout(dig.hide,4000);
		});
	},
	
	setDigData : function(ans){
		var set_survey_data = 'http://users.qzone.qq.com/cgi-bin/survey/set_survey_data.cgi',
		dig = QZONE.widget.dig,digc = QZONE.widget.dig.defaultConfig,
		fs = new QZFL.FormSender(set_survey_data, "post", {uin:digc.uin,id:digc.id,ans:ans,oz:digc.oz}, "utf-8");
		fs.onSuccess = function(re){
			if(re.sum != undefined&&re.ret!="error"){
				re.answer = ans;
				dig.renderDigData(re,true);
				if(ans==1){
					dig.addOne("up");
				}else{
					dig.addOne("down");
				}
			}else if(re.ret == "error"){
				if(re.type =='login'){
					QZONE.FP.showLoginBox("mall");
				}else{
					dig.showMsgbox(re.msg,1,2000);
				}
			}
		};
		fs.onError = function(){
			dig.showMsgbox("操作失败，请稍后再试。",1,3000);
		};
		fs.send();
	},
	
	show : function(e,up,down){
		var dig = QZONE.widget.dig,tup,tdown;
		if(dig._showed){
			QZFL.css.removeClassName($('dig-result'),"none");
			$("dig-triangle").innerHTML = "▲";
			dig._showed = false;
			if(dig.defaultConfig.tween){
				/*tup = new QZFL.Tween($("t-up"), "width", null, "0px", up*168/100+"px", 0.25);
				tup.start();
				tdown = new QZFL.Tween($("t-down"), "width", null, "0px",down*168/100+"px", 0.5);
				tdown.start();*/
				QZFL.dom.setStyle($("t-up"),'width','0px');
				QZFL.dom.setStyle($("t-down"),'width','0px');
				QZFL.effect.run($("t-up"),{width:up*168/100}, {duration:1000});
				QZFL.effect.run($("t-down"),{width:down*168/100}, {duration:1000});
			}
		}else{
			dig.hide();
		}
	},
	
	hide : function(){
		var dig = QZONE.widget.dig,e=QZFL.event.getTarget();
		QZFL.css.addClassName($('dig-result'),"none");
		$("dig-triangle").innerHTML = "▼";
		dig._showed = true;
	},
	
	addOne : function(type){
		var ele = type + "-add",add;
		$(ele).style.display="block";
		/*var add = new QZFL.Tween($(ele), "top", null, "0px", "-15px", 0.5);
		add.start();
		add.onMotionStop = function(){
			setTimeout(function(){$(ele).style.display="none";},500);
		};*/
		QZFL.effect.run($(ele), {
    	  opacity: 0,
    	  top:'-20'
        }, {
        	duration : 500
      	});
	},
	
	fourFive : function (a,total){
		var floor = Math.floor(a/total*100),res;
		if(a/total*100-floor>0.5){
			res = floor+1;
		}else{
			res = floor;
		}
		return res;
	},
	
	getTotalTips : function(total){
		var m = total/10000,tips="";

		if(m<=1){
			tips = "大家";
		}else if(m>1&&m<10000){
			tips = "超过"+Math.floor(m) +"万人";
		}else if(m>=10000){
			tips = "超过"+Math.floor(m/10000) +"亿人";
		}
		return tips;
	},
	
	
	getNumShow : function(count){
		var show = '';
		
		if(count>9999999&&count<100000000){
			show = Math.floor(count/10000)+"万\+";
		}else if(count>99999999){
			show = Math.floor(count/100000000)+"亿\+";
		}else{
			show = count;
		}
		
		return show;
	},
	
	sendPV : function(path){
		var pvCurDomain = "widgetdig.qzone.qq.com";
		var pvCurUrl = "/" + (path==1?'about':'suggest');
		if (typeof(QZONE.FP._t.QZONE.statistic.pvPing.pgvMainV5) == "function") {
			QZONE.FP._t.QZONE.statistic.pvPing.pgvMainV5(pvCurDomain, pvCurUrl);
		}
	},
	
	/**
	 * 显示消息
	 */
	showMsgbox : (QZONE.FP.showMsgbox || parent.QZONE.FrontPage.showMsgbox || QZONE.FrontPage.showMsgbox)
};/*  |xGv00|e3023e7a774e5ebbfd16df8a02837d5c */